/*
 * Copyright (c) 2005 - 2011 CAS Dev Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the CAS Dev. Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      ASSQLConnectorObject.hpp
 *
 * $CAS$
 */
#ifndef _AS_SQL_CONNECTOR_OBJECT_HPP__
#define _AS_SQL_CONNECTOR_OBJECT_HPP__ 1

/**
  @file ASSQLConnectorObject.hpp
  @brief Application server example object
*/
#include <ASObject.hpp>

#include <STLString.hpp>

using namespace CAS;

#define C_SIGMA_CRITERIA 3.0

namespace SQL
{
// FWD
class Connection;
class ConnectionBase;
}

namespace CAS_MOD
{

/**
  @class ASSQLConnectorObject ASSQLConnectorObject.hpp <ASSQLConnectorObject.hpp>
  @brief Application server SQL connection object
*/
class ASSQLConnectorObject:
  public ASObject
{
public:

	/**
	  @brief Connection statistics
	*/
	struct ConnectorStatistics
	{
		W_FLOAT      conn_avg_time;
		W_FLOAT      conn_dispersion;

		W_FLOAT      query_avg_time;
		W_FLOAT      query_dispersion;
	};

	/**
	  @brief Constructor
	*/
	ASSQLConnectorObject(const UINT_32 iIErrorTimeout = 60);

	/**
	  @brief Add server to pool
	  @param sDriver - database driver
	  @param sCredentials - connection credentials
	*/
	INT_32 AddConnection(const STLW::string  & sDriver,
	                     const STLW::string  & sCredentials);

	/**
	  @brief Get pointer to the object
	*/
	SQL::Connection * GetPermConnection();

	/**
	  @brief Get pointer to the object
	*/
	SQL::Connection * GetConnection();

	/**
	  @brief Get connection statistics
	*/
	const ConnectorStatistics & GetStatistics() const;

	/**
	  @brief A virtual destructor
	*/
	~ASSQLConnectorObject() throw();

private:
	struct ConnectionPair
	{
		/** Connection driver      */
		SQL::ConnectionBase  * driver;
		/** Connection             */
		SQL::Connection      * connection;
		/** Timestamp              */
		W_FLOAT                ts;

		ConnectionPair(SQL::ConnectionBase  * pDriver,
		               SQL::Connection      * pConnection);
	};
	/** Error timeout                       */
	UINT_32                          iErrorTimeout;
	/** Connector statistics                */
	ConnectorStatistics              oConnectorStatistics;

	/** List of SQL connections             */
	STLW::vector<ConnectionPair>     vConnections;
	/** List of per-session SQL connections */
	STLW::vector<SQL::Connection *>  vRequestConnections;

	// DOes not exist
	ASSQLConnectorObject(const ASSQLConnectorObject & oRhs);
	ASSQLConnectorObject & operator=(const ASSQLConnectorObject & oRhs);
	/**
	  @brief Get object type
	  @return human-readable type of object
	*/
	CCHAR_P GetObjectType() const;

	/**
	  @brief Get object name
	  @return human-readable name of object
	*/
	CCHAR_P GetObjectName() const;

	/**
	  @brief Get connection to sql server
	*/
	SQL::Connection * GetServer();

	/**
	  @brief Get connection to sql server
	*/
	SQL::Connection * GetConnnectionFromRange(const UINT_32  iStartPos,
	                                          const UINT_32  iEndPos,
	                                          const bool     bUseStatistics);

	/**
	  @brief Handle event
	  @param iEventId - event ID
	  @return HANDLER_OK - if success, HANDLER_ERROR - if any error occured, HANDLER_DONE - if no futher processing need
	*/
	INT_32 HandleEvent(const UINT_32 iEventId);
};

} // namespace CAS_MOD
#endif // _AS_SQL_CONNECTOR_OBJECT_HPP__
// End.
